package com.shiji.yimai.base.fragment;

import android.content.Context;
import android.view.View;

import com.shiji.yimai.base.presenter.BaseContract;
import com.shiji.yimai.widget.LayoutStatusManager;

/**
 * Created by 郑磊 on 2018/5/27.
 */

public abstract class PresenterFragment<P extends BaseContract.Presenter> extends BaseFragment implements BaseContract.View<P> {

    protected P mPresenter;
    protected LayoutStatusManager mLayoutStatusManager;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mPresenter = initPresenter();
    }

    /**
     * 初始化Presenter
     *
     * @return Presenter
     */
    protected abstract P initPresenter();

    @Override
    public void onDetach() {
        super.onDetach();
        if(mPresenter!=null){
            mPresenter.destroy();
            mPresenter=null;
        }
    }


    @Override
    protected void initView() {
        super.initView();
        mLayoutStatusManager = new LayoutStatusManager(getContext(),getSuccessView());
        setStatusLoading();
    }

    protected abstract View getSuccessView();

    /**
     * 显示错误布局
     */
    @Override
    public void setStatusError(){
        mLayoutStatusManager.setStatus(LayoutStatusManager.STATUS_ERROR);
    }
    /**
     * 显示加载中布局
     */
    @Override
    public void setStatusLoading(){
        mLayoutStatusManager.setStatus(LayoutStatusManager.STATUS_LOADING);
    }
    /**
     * 显示成功布局
     */
    @Override
    public void setStatusSuccess(){
        mLayoutStatusManager.setStatus(LayoutStatusManager.STATUS_SUCCESS);
    }
    /**
     * 显示加载中布局
     */
    @Override
    public void setStatusEmpty(){
        mLayoutStatusManager.setStatus(LayoutStatusManager.STATUS_EMPTY);
    }



}
